package org.rqb.panel;

import lombok.extern.slf4j.Slf4j;
import org.rqb.frame.MyFrame;
import org.rqb.panel.partPanel.AdPanel;
import org.rqb.ui.panel.BackgroundPanel;
import org.rqb.ui.button.MyGlassButton;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

@Slf4j
public class MenuPanel extends JPanel implements ActionListener {

    private final CardLayout cardLayout;
    private final JPanel cardPanel;
    private final MyFrame myFrame;

    private MyGlassButton repeatBtn;
    private MyGlassButton signBtn;
    private MyGlassButton queryLanBtn;
    private MyGlassButton pointTransBtn;
    private MyGlassButton m3u8Btn;
    private MyGlassButton clickFrequencyBtn;

    private BackgroundPanel centerPanel; // ✅ 背景图片仅绘制在这个面板

    public MenuPanel(JPanel cardPanel, CardLayout cardLayout, MyFrame myFrame) {
        this.myFrame = myFrame;
        this.cardLayout = cardLayout;
        this.cardPanel = cardPanel;

        setLayout(new BorderLayout());

        btnInit();   // 中心按钮面板（带背景图）
        adInit();    // 右侧广告面板
    }

    private void adInit() {
        AdPanel adPanel = new AdPanel();
        adPanel.setOpaque(true); // ✅ 保证广告面板不透明（不显示背景）
        this.add(adPanel, BorderLayout.EAST);
    }

    private void btnInit() {
        // ✅ 创建一个带背景的 centerPanel
        centerPanel = new BackgroundPanel("/images/menuBg.png"); // 只显示这张图片的背景
        centerPanel.setLayout(new GridLayout(5, 3, 10, 10));
        centerPanel.setOpaque(false);

        repeatBtn = createButton("复现");
        signBtn = createButton("签名");
        queryLanBtn = createButton("查询局域网设备");
        pointTransBtn = createButton("节点转换");
        m3u8Btn = createButton("m3u8下载");
        clickFrequencyBtn = createButton("点击频率");

        centerPanel.add(repeatBtn);
        centerPanel.add(signBtn);
        centerPanel.add(queryLanBtn);
        centerPanel.add(pointTransBtn);
        centerPanel.add(m3u8Btn);
        centerPanel.add(clickFrequencyBtn);

        add(centerPanel, BorderLayout.CENTER);
    }

    private MyGlassButton createButton(String text) {
        MyGlassButton btn = new MyGlassButton(text);
        btn.addActionListener(this);
        return btn;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == repeatBtn) repeatBtnOnClick();
        else if (source == signBtn) signBtnOnClick();
        else if (source == queryLanBtn) queryLanBtnOnClick();
        else if (source == pointTransBtn) pointTransBtnOnClick();
        else if (source == m3u8Btn) m3u8BtnOnClick();
        else if (source == clickFrequencyBtn) clickFrequencyBtnOnClick();
    }

    private void clickFrequencyBtnOnClick() {
        cardLayout.show(cardPanel, "clickFrequencyPanel");
    }

    private void m3u8BtnOnClick() {
        cardLayout.show(cardPanel, "m3u8Panel");
    }

    private void pointTransBtnOnClick() {
        cardLayout.show(cardPanel, "proxyPointPanel");
    }

    private void queryLanBtnOnClick() {
        cardLayout.show(cardPanel, "queryLANDevicesPanel");
    }

    private void signBtnOnClick() {
        cardLayout.show(cardPanel, "signPanel");
    }

    private void repeatBtnOnClick() {
        cardLayout.show(cardPanel, "repeatPanel");
    }

}
